package com.hzjy.api.controller;


import com.hzjy.api.beans.HzjyProduct;
import com.hzjy.api.beans.HzjyProject;
import com.hzjy.api.beans.HzjyServiceCharge;
import com.hzjy.api.beans.HzjyWorks;
import com.hzjy.api.service.BqTokenService;
import com.hzjy.api.service.impl.ApiProductServiceImpl;
import com.hzjy.api.service.impl.HzjyServiceChargeServiceImpl;
import com.hzjy.api.vo.BqLoginUser;
import com.hzjy.api.vo.BqUser;
import com.hzjy.common.core.domain.AjaxResult;
import com.hzjy.common.utils.ServletUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api("我的挂牌")
@RestController
@RequestMapping("/api/wxMyTrading")
public class WxMyTradingController {

    @Autowired
    private ApiProductServiceImpl apiProductService;

    @Autowired
    private BqTokenService bqTokenService;

    @Autowired
    private HzjyServiceChargeServiceImpl hzjyServiceChargeService;

    @ApiOperation("添加产品")
    @PostMapping("/addWxMyProduct")
    private AjaxResult addWxMyTrading(@RequestBody HzjyProduct hzjyProduct){
        BqLoginUser loginUser = bqTokenService.getBqLoginUser(ServletUtils.getRequest());
        if (loginUser == null) {
            return AjaxResult.error(401, "未登录");
        }
        BqUser bqUser = loginUser.getBqUser();
        Long userId = bqUser.getUserId();
        hzjyProduct.setUserId(Integer.parseInt(userId.toString()));
        Long productId = apiProductService.addTradingByBeans(hzjyProduct);
        return AjaxResult.success(200,"成功",productId);
    }

    @ApiOperation("修改产品")
    @PostMapping("/updateWxMyTrading")
    private AjaxResult updateWxMyTrading(@RequestBody HzjyProduct hzjyProduct){
        BqLoginUser loginUser = bqTokenService.getBqLoginUser(ServletUtils.getRequest());
        if (loginUser == null) {
            return AjaxResult.error(401, "未登录");
        }
        BqUser bqUser = loginUser.getBqUser();
        Long userId = bqUser.getUserId();
        hzjyProduct.setUserId(Integer.parseInt(userId.toString()));
        hzjyProduct.setPrice(hzjyProduct.getPrice() * 100);
        apiProductService.updateHzjyProduct(hzjyProduct);
        return AjaxResult.success();
    }

    @ApiOperation("添加作品")
    @PostMapping("/addWxWorks")
    private AjaxResult addWxWorks(@RequestBody HzjyWorks hzjyWorks){
        Long worksId = apiProductService.insertHzjyWorks(hzjyWorks);
        return AjaxResult.success(200,"成功",worksId);
    }

    @ApiOperation("修改作品")
    @PostMapping("/updateWxWorks")
    private AjaxResult updateWxWorks(@RequestBody HzjyWorks hzjyWorks){
        apiProductService.updateWxWorks(hzjyWorks);
        return AjaxResult.success();
    }

    @ApiOperation("添加项目")
    @PostMapping("/addWxProject")
    private AjaxResult addWxProject(@RequestBody HzjyProject hzjyProject){
        Long projectId = apiProductService.addWxProject(hzjyProject);
        return AjaxResult.success(200,"成功",projectId);
    }
    @ApiOperation("修改项目")
    @PostMapping("/updateWxProject")
    private AjaxResult updateWxProject(@RequestBody HzjyProject hzjyProject){
        apiProductService.updateWxProject(hzjyProject);
        return AjaxResult.success();
    }

    @ApiOperation("小程序审核")
    @PostMapping("/listStatus")
    private AjaxResult listStatus(){
        HzjyServiceCharge hzjyServiceCharge = new HzjyServiceCharge();
        List<HzjyServiceCharge> hzjyServiceCharges = hzjyServiceChargeService.selectHzjyServiceChargeList(hzjyServiceCharge);
        Integer wxStatus = hzjyServiceCharges.get(0).getWxStatus();
        return AjaxResult.success(200,"成功",wxStatus);
    }

}
